<?php
namespace Admin\Service;
//基础类
use Admin\AppConst\WebConst;
use Admin\Help\Cache;
use Admin\Help\ReturnMessage;


class BaseService
{
    protected $redis;               //redis对象

    /**
     * @function    map         获取sku列表数据
     * @param       array       $data  数据
     * @param       string      $field key
     * @param       string      $value value default null
     * @author      zhangle
     * @date        2018-07-13
     * @return      array
     */
    protected function toMap(array $data, $field = 'id', $value = WebConst::DEFAULT_TYPE_EMPTY){
        $result  = array();

        if(empty($data)){
            return $result;
        }

        //开始处理数据
        foreach ($data as $item) {
            $val = $item;
            if(!empty($value)){
                $val   = $item[$value];
            }
            $result[$item[$field]]  = $val;
        }

        return  $result;
    }



    /* @function    getRedisObject        获取redis对象
     * @author:     zhangle
     * @date:       2018/07/17
     * @return      object
     */
    protected function getRedisObject(){

        if (!$this->redis) {
            $this->redis = new Cache();
        }
        return $this->redis;
    }

    /* @function    getFieldFromArray 从数组中获取某个字段的所有值
     * @param       $data       array   数据源
     * @param       $field      string  字段
     * @author:     zhangle
     * @date:       2018/07/27
     * @return      array
     */
    protected function getFieldFromArray($data, $field = 'id'){

        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;

        if(empty($data)){
            return $result;
        }

        return array_unique(array_values(array_column($data, $field)));
    }


}